home *** CD-ROM | disk | FTP | other *** search
- From: saa@ecofin.ch (Angelo Salvade)
- Message-ID: <00614.2912664663.294@ecofin.uucp>
- X-Original-Date: Thu, 18 Apr 1996 9:30:03 -0100
- Path: in1.uu.net!bounce-back
- Date: 19 Apr 96 14:43:42 GMT
- Approved: fjh@cs.mu.oz.au
- Organization: -
- Newsgroups: comp.std.c++
- X-Umcp-Priority: 1
- Subject: Are these "implicit" casts legal?
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMXem6eEDnX0m9pzZAQGSFwF/UuBIB5ms4HHVdDsyxxcILLKlyRuyK4U4
- QrSkrapo/zvZkTbnD0YFmFOfBVHZX4ry
- =eg4z
-
- Can anybody tell me if the following code is legal:
-
- //----------------------------------------------------------------
-
- class A {};
-
- class B: public A {};
-
- //----------------------------------------------------------------
-
- // with templates
-
- template <class O>
- class Ref {
- public:
- Ref();
- Ref(O* value);
- operator O*();
- };
-
- extern void f1(Ref<A> a);
-
- static void t1()
- {
- Ref<B> b;
- Ref<A> a(b); // #1
-
- f1(b); // #2
- }
-
- //----------------------------------------------------------------
-
- // without templates
-
- class RefA {
- public:
- RefA(A* value);
- };
-
- class RefB {
- public:
- RefB();
- operator B*();
- };
-
- extern void f2(RefA a);
-
- static void t2()
- {
- RefB b;
- RefA a(b); // #3
-
- f2(b); // #4
- }
-
- //----------------------------------------------------------------
-
- I believe that #1-4 should work identically and are correct C++:
- A Ref<A> should be initialised with a Ref<B>.
- The following steps should happen:
- - call of Ref<B>.operator B*()
- - C++ built in conversion of B* to A*
- - call of constructor Ref<A>(A*)
-
- I checked this code with the Metrowerks CW8,
- GNU 2.7.2 and SunPro C++ 4.0.1 & C++ 4.1 compilers.
- The SunPro C++ 4.1 is the only compiler of those
- that didn't compile #2 and #4. It gave the following message:
- "Error: Cannot use Ref<B> to initialize Ref<A>".
-
- Is my code illegal or is this a bug in the compiler?
-
- Angelo Salvade
- salvade@ecofin.ch
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-